package 日期算法.一年中的第几天;

/**
 * TODO
 *
 * @className: ynzddjt
 * @description: TODO
 * @author: 等什么柠檬君
 * @date: 2022-05-05 18:58
 **/
public class ynzddjt {
    public static void main(String[] args) {


        System.out.println(dayOfYear("2019-01-09"));
        System.out.println(dayOfYear("2019-02-10"));
        System.out.println(dayOfYear("2016-02-09"));
    }

    public static int dayOfYear(String date) {
        int[] monthDays =  { 0,
                31, 28, 31, 30, 31, 30,
                31, 31, 30, 31, 30, 31
        };
        final String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);
        int day = Integer.parseInt(split[2]);
        if (month == 1){
            return day;
        }
        if (isLeapYear(year)){
            for (int i = 1; i < month; i++) {
                day += monthDays[i];
            }
            if (month > 2){
                ++day;
            }
        }else {
            for (int i = 1; i < month; i++) {
                day += monthDays[i];
            }
        }

        return day;
    }
    public static boolean isLeapYear(int y){
        return ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));
    }
}
